package com.coment.po.roles;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.constant.AIDCFG;
import com.coment.game.fight.Attribute;
import com.coment.game.fight.AttributeManagerLogic;
import com.coment.game.fight.Attrs;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Transient;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 角色养成面板
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(of = {"userInfoId", "cardId", "base", "inventory"})
public class CultivatePanel {
    /// 版本
    private int version;

    /// 用户id
    private int userInfoId;

    /// 卡片id
    public int cardId;

    /// 角色基础属性
    @JsonIgnore
    public Map<Integer, Double> base = new Attrs();

    /// 角色物品栏 k:物品分类 v:对应的道具 1-12 对应装备
    public Map<Integer, Set<Integer>> inventory = new HashMap<>();

    @JsonIgnore
    @Transient
    public Map<Integer, Double> value;


    public CultivatePanel(Integer userInfoId, Integer cardId, Integer level) {
        this.version = Tables.version;
        this.userInfoId = userInfoId;
        if (cardId > 1000) {
            level = Tables.cfg.get_tbmonsterconfig().get(cardId).level;
        }
        this.cardId = cardId;
        this.base = Attribute.obtainRolesBaseValue();
        this.base.put(AIDCFG.level, Double.valueOf(level));
    }

    @JsonIgnore
    /// 获取养成系全属性
    public Attrs getValue() {
        int level = this.base.get(AIDCFG.level).intValue();

        // 角色养成属性
        Attrs result = new Attrs(this.base);

        // 装备属性
        this.inventory.forEach((key, value) ->
                value.forEach(id ->
                        AttributeManagerLogic.inventoryAttrs.get(id).forEach((key1, value1) -> result.merge(key1, value1, Double::sum)
                        )
                )
        );
//            log.info("角色养成属性:{}", Attribute.getLogInfo(result));

        // 星级属性
        Attrs starValue = Attribute.obtainRolesStarValue(cardId, this.base.get(AIDCFG.star));
//            log.info("星级属性:{}", Attribute.getLogInfo(starValue));

        // 获取 a m 数值
        starValue.obtainByType(
                2, 3
        ).forEach((key, value) ->
                result.merge(key, value, Double::sum)
        );

        // 返回 养成系全属性{包含全局属性}
        return Attribute.obtainCardAttrs(
                userInfoId,
                cardId,
                level,
                result
        );
    }
}
